


/*

Tables B6, B7, other, B10, other and B11

*/


local nametable = "tables/rev_potentialfailure"


forvalues xd=1(1)3{
	
	if `xd'==1{
		local xdist = 50
	}
	if `xd'==2{
		local xdist = 25
	}
	if `xd'==3{
		local xdist = 100
	}

	*-----
	eststo clear
	local column_number = 1
	local column_name = "column" + "`column_number'"
	*---	
	
	forvalues xlin = 0(1)1{
	

	if `xlin'==0{
		local poly = "No"
		local xcontrols 	= "  "
	}
	if `xlin'==1{	
		local poly = "Linear"
		local xcontrols		= " rdist1*   "
	}
	
	use "public_data/g30.dta", clear

	local varlistc = "cassava_caparhigh cocoa_caparhigh cotton_caparhigh groundnut_caparhigh maize_caparhigh millet_caparhigh spotato_caparhigh phaseolus_caparhigh rice_caparhigh sorghum_caparhigh"

	foreach var of varlist `varlistc'{

	
		use "public_data/g30.dta", clear
	
		drop if adist>`xdist'
	
		drop if `var'==.
		gen inter = index("`var'","_")
		local crop = substr("`var'",1,inter-1)
		local crop = proper("`crop'")
		local crop2 = " " 
		local crop3 = " " 
		drop inter
		if "`var'"=="sugarcane_caparhigh"{
			local crop = "Sugar"
			local crop2 = "cane"
		}
		if "`var'"=="sugarbeet_caparhigh"{
			local crop = "Sugar"
			local crop2 = "beet"
		}
		if "`var'"=="wpotato_caparhigh"{
			local crop = "Potato"
		}
		if "`var'"=="spotato_caparhigh"{
			local crop = "Sweet"
			local crop2 = "Potato"
		}


		local crop_null = subinstr("`var'","_caparhigh","_null",.)
		gen interdep = `crop_null'		

		replace weight = 1

		*computes mean of dependent var
		egen interm = mean(interdep)
		local vmeandep = interm[1]
		local meandep = string(`vmeandep')
		if index("`meandep'",".")==1{
			local meandep = "0"+substr("`meandep'",1,3)
		}
		if index("`meandep'",".")<5&index("`meandep'",".")>1{
			local meandep = substr("`meandep'",1,4)
		}
	

		*computes the number of country pairs
		egen intercp = group(border)
		egen intercpmax = max(intercp)
		local countrypair = intercpmax[1]
		drop intercp*			
	
	
*		xi: areg interdep `xcontrols' varofi  [aw=weight],  a(groupfe) cluster(border)
		reghdfe interdep `xcontrols' varofi [aw=weight], absorb(groupfe) vce(cluster groupfe country)
		eststo `column_name'
		estadd local space  				" "
		estadd local crop  					"`crop'"
		estadd local crop2  				"`crop2'"
		estadd local countrypair 				"`countrypair'"
		estadd local maxdist 					"`xdist'"
		estadd local polynomial				"`poly'"
		estadd local nmeandep			"`meandep'"
		estadd local groupfe				"Yes"
		local column_number = `column_number' + 1
		local column_name = "column" + "`column_number'"
	}
	}


	

label var interdep  				" "		
local nametable1 = "`nametable'" + "`xd'" + ".tex"

local depvar= "\textit{Dependent Variable: Probablity of crop failure}"

#delimit ;
esttab column1 column2 column3 column4 column5 column6 column7 column8 column9 column10
using "`nametable1'", replace
label
b(3)
se(3)
star(* 0.10 ** 0.05 *** 0.01)
width(1.0\hsize)
nonotes
nolines
noconstant
mgroups("`depvar'", pattern(1 0 0 0 0 0 0 0 0)
prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) 
keep(varofi)
order(varofi)
  stats(groupfe					space	polynomial			space	r2		N					countrypair			space	maxdist							space	crop		crop2	space	nmeandep,
    fmt(0						0		0					0		3		0					0					0		0								0		0			0		0		0)
layout(@						@		@					@		@		@					@					@		@								@		@			@		@		@)	 	
labels("Border x Ethnic FE"		" "		"RD polynomial"		" "		"R2"	"\# Observations" 	"\# Country pairs"	" "		"Max distance to border (km)"	" "		"Crop"		" "		" "		"Mean Dep. Var.")
 )	
noobs
booktabs
;
#delimit cr


label var interdep  				" "		
local nametable1 = "`nametable'" + "lin" + "`xd'" + ".tex"

local depvar= "\textit{Dependent Variable: Probablity of crop failure}"

#delimit ;
esttab column11 column12 column13 column14 column15 column16 column17 column18 column19 column20
using "`nametable1'", replace
label
b(3)
se(3)
star(* 0.10 ** 0.05 *** 0.01)
width(1.0\hsize)
nonotes
nolines
noconstant
mgroups("`depvar'", pattern(1 0 0 0 0 0 0 0 0)
prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) 
keep(varofi)
order(varofi)
  stats(groupfe					space	polynomial			space	r2		N					countrypair			space	maxdist							space	crop		crop2	space	nmeandep,
    fmt(0						0		0					0		3		0					0					0		0								0		0			0		0		0)
layout(@						@		@					@		@		@					@					@		@								@		@			@		@		@)	 	
labels("Border x Ethnic FE"		" "		"RD polynomial"		" "		"R2"	"\# Observations" 	"\# Country pairs"	" "		"Max distance to border (km)"	" "		"Crop"		" "		" "		"Mean Dep. Var.")
 )	
noobs
booktabs
;
#delimit cr

}
